/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Drobny
 */
public class RotationDialog extends javax.swing.JDialog {

    private RotationProcess proc = new RotationProcess();
    private final BufferedImage orygImg; //used for getting proper center x and y of original image
    private final BufferedImage image;
    private ImagePanel imgPanel;
    private AppControler ctrl;
    private RotationCenter center;
    private int angle;
    private int centerX=0,centerY=0;
    private int wSpace=50,hSpace=170; //GUI & angleRotation area
    
    public RotationDialog(java.awt.Frame parent, boolean modal, BufferedImage img, AppControler c) {
        super(parent, modal);
        initComponents();
        ctrl = c;
        orygImg=img;
        imgPanel = ImagePanel.getSmallImagePanel(img);
        image = imgPanel.getImage();
        
        int w=image.getWidth(); 
        int h=image.getHeight();
        
        panel.setSize(w,h);
        panel.setLayout(new GridLayout(1, 1));
        
        imgPanel.setSize(w,h);
        
        centerX=(int)w/2;
        centerY=(int)h/2;
        center=new RotationCenter();
        center.setSize(w, h);
        
        layerPanel.add(imgPanel, new Integer(0));
        layerPanel.add(center, new Integer(1));
        layerPanel.setSize(w, h);
        
        panel.add(layerPanel,0);
        
        setSize(w+wSpace, h+hSpace);
        //setSize(image.getWidth()+50, image.getHeight()+170);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        processBtn = new javax.swing.JButton();
        rotate90Btn = new javax.swing.JButton();
        rotate180Btn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        angleSlider = new javax.swing.JSlider();
        angleSpinner = new javax.swing.JSpinner();
        rotate270Btn = new javax.swing.JButton();
        panel = new javax.swing.JPanel();
        layerPanel = new javax.swing.JLayeredPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(372, 350));

        processBtn.setText("Korektuj");
        processBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processBtnActionPerformed(evt);
            }
        });

        rotate90Btn.setText("Obrót o 90\u00B0 w prawo");
        rotate90Btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rotate90BtnActionPerformed(evt);
            }
        });

        rotate180Btn.setText("Obrót o 180");
        rotate180Btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rotate180BtnActionPerformed(evt);
            }
        });

        jLabel1.setText("kąt obrotu");

        angleSlider.setMaximum(359);
        angleSlider.setValue(0);
        angleSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                angleSliderStateChanged(evt);
            }
        });

        angleSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 359, 1));
        angleSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                angleSpinnerStateChanged(evt);
            }
        });

        rotate270Btn.setText("Obrót o 90\u00B0 w lewo");
        rotate270Btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rotate270BtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(layerPanel)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(layerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rotate180Btn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(angleSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(angleSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(processBtn))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rotate90Btn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rotate270Btn)))
                        .addGap(0, 5, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotate90Btn)
                    .addComponent(rotate270Btn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotate180Btn)
                    .addComponent(jLabel1)
                    .addComponent(angleSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(processBtn)
                    .addComponent(angleSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void processBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processBtnActionPerformed
        angle=(int)angleSlider.getValue();
        double wCenterScale=(double)centerX/(double)image.getWidth();
        double hCenterScale=(double)centerY/(double)image.getHeight();
        proc.setParams(angle, (int)(orygImg.getWidth()*wCenterScale), (int)(orygImg.getHeight()*hCenterScale));
        
        ctrl.setProcess(proc);
        ctrl.process();
        dispose();
    }//GEN-LAST:event_processBtnActionPerformed

    private void rotate90BtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotate90BtnActionPerformed
        angle=(int)angleSlider.getValue();
        angle=((angle+90)<360) ? angle+90 : angle+90-360 ;
        rotationAction();
        angleSlider.setValue(angle);
        angleSpinner.setValue(angle);
    }//GEN-LAST:event_rotate90BtnActionPerformed

    private void rotate180BtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotate180BtnActionPerformed
        angle=(int)angleSlider.getValue();
        angle=((angle+180)<360) ? angle+180 : angle+180-360 ;
        rotationAction();
        angleSlider.setValue(angle);
        angleSpinner.setValue(angle);
    }//GEN-LAST:event_rotate180BtnActionPerformed

    private void angleSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_angleSliderStateChanged
        angle=(int)angleSlider.getValue();
        angleSpinner.setValue(angle);
        rotationAction();
    }//GEN-LAST:event_angleSliderStateChanged

    private void angleSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_angleSpinnerStateChanged
        angle= Integer.parseInt(angleSpinner.getValue().toString());
        angleSlider.setValue(angle);
        rotationAction();
    }//GEN-LAST:event_angleSpinnerStateChanged

    private void rotate270BtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotate270BtnActionPerformed
        angle=(int)angleSlider.getValue();
        angle=((angle+270)<360) ? angle+270 : angle+270-360 ;
        rotationAction();
        angleSlider.setValue(angle);
        angleSpinner.setValue(angle);
    }//GEN-LAST:event_rotate270BtnActionPerformed
    
    /**
     * sets parameters in process
     * process
     * sets sizes
     */
    private void rotationAction(){
        proc.setParams(angle, centerX, centerY);
        BufferedImage tmpImage = proc.process(image);
        imgPanel.setImage(tmpImage);
        imgPanel.setLocation(0, 0);
        int w=tmpImage.getWidth();
        int h=tmpImage.getHeight();
        imgPanel.setSize(w, h);
        panel.setSize(w, h);
        layerPanel.setSize(w, h);
        setWindowSize(w+wSpace, h+hSpace);
        center.circle.setX((int)(w/2));
        center.circle.setY((int)(h/2));
        center.repaint();
        //if((tmpImage.getWidth()+centerX+wSpace)>size && (tmpImage.getHeight()+centerY+hSpace)>size)
            //setSize(tmpImage.getWidth()+centerX+wSpace, tmpImage.getHeight()+centerY+hSpace);
    }
    
    private void setWindowSize(int w, int h){
        int ow = getWidth();
        int oh = getHeight();
        int nw, nh;
        if(ow < w ){
            nw = w;
        }else{
            nw = ow;
        }
        if(oh < h ){
            nh = h;
        }else{
            nh = oh;
        }
        setSize(nw, nh);
    }
    
    class RotationCenter extends JPanel {
        public final int ovalSize=20;
        public DraggableCircle circle=new DraggableCircle(centerX,centerY, ovalSize);
        
        public RotationCenter(){
            setBackground(new Color(0,0,0,0));
            setOpaque(false);
            this.addMouseMotionListener(new MouseMotionListener() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    //circle.setX(e.getX());
                    //circle.setY(e.getY());
                    //getParent().repaint();
                    //repaint();
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                }
            });
            this.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {
                }
                @Override
                public void mousePressed(MouseEvent e) {
                    //circle.setX(e.getX());
                    //circle.setY(e.getY());
                    //repaint();
                }
                @Override
                public void mouseReleased(MouseEvent e) {
                    //centerX=e.getX();
                    //centerY=e.getY();
                    //repaint();
                }
                @Override
                public void mouseEntered(MouseEvent e) {
                }
                @Override
                public void mouseExited(MouseEvent e) {
                }
            });
        }
        
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(new Color(99, 131, 255));
            circle.draw(g2d);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider angleSlider;
    private javax.swing.JSpinner angleSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane layerPanel;
    private javax.swing.JPanel panel;
    private javax.swing.JButton processBtn;
    private javax.swing.JButton rotate180Btn;
    private javax.swing.JButton rotate270Btn;
    private javax.swing.JButton rotate90Btn;
    // End of variables declaration//GEN-END:variables
}
